<?php
require_once '../../a_config.php';
require_once "{$A_CONFIG['api_include']}";
require_once '../../api_header.php';
$conn = connect_to_db();
$err = -1;
$msg = "";
$page_num = 10;//每页显示数量
$condition = array();
if (isset($_POST['token']) && !empty($_POST["token"]) &&
    isset($_POST['time']) && !empty($_POST["time"]) &&
    isset($_POST["nextPage"]) && !empty($_POST["nextPage"]) &&
    isset($_POST['search'])) {
    $key = $A_CONFIG['api_key'];
    $unixtime = $_POST['time'];
    $token = md5($key . $unixtime);
    if ($token == $_POST["token"]) {
        $search = mysql_real_escape_string($_POST["search"]);
        $nextPage = mysql_real_escape_string($_POST["nextPage"]);
        $range = getRange($nextPage);  //获取limit条件
        $regSpace = '/^\s+$/';
        if ($search != "" && !preg_match($regSpace, $search)) {
            $condition[]="(`box_id` like '%{$search}%' )";
        }
        $condition[]="is_del=0";
        $where = '';
        if (!empty($condition)) {
            foreach ($condition as $key => $val) {
                if ($key == 0) {
                    $where .= "where $val";
                } else {
                    $where .= " and $val";
                }
            }
        }
        $s_db_name = $A_CONFIG['project_pre'] . '_tcp_status';
        $sql = "SELECT * FROM `{$s_db_name}` $where order by id desc LIMIT $range,$page_num";
        $res = mysql_query($sql, $conn);
        if (is_resource($res)) {
            $info = array();
            while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
                $item = array();
                $item["info"] = $row;
                $info[] = $item;
            }
            $err = 0;
            $msg = $info;
        } else {
            $msg = "数据处理出错";
        }
    } else {
        $msg = "用户网点获取失败，请刷新页面";
    }

} else {
    $err = -5;
    $msg = "参数错误";
}
if (!$A_CONFIG['is_debug']) {
    $resArr = compact("err", "msg");
} else {
    $resArr = compact("err", "msg", 'sql');
}

$resJson = json_encode_cn($resArr);
echo $resJson;
function getRange($nextPage)
{
    $result = 0;
    if ($nextPage > 0) {
        if (is_numeric($nextPage)) {
            $result = ((int)$nextPage - 1) . "0";
        }
    }
    return (int)$result;
}
